<?php
require_once "ModelBase.php";      
class Admin extends TTEvent_Table_Abstract
{
    protected $_name = 'tamtay_event';
    protected $_primary = 'eid';
    
        public function getListEvent($search,$page,$limit=10){
        $eid=$search['eid'];
        $event_name=$search['event_name'];
        $uid=$search['uid'];
        $published=$search['published'];
        $promote=$search['promote'];
        $cid=$search['cid'];
        
        $where = "";
        if($eid!=""){
            $where = "e.eid=".$eid;
        }
        if($event_name!=""){
            $where .= ($where!=""?" AND ":"")."e.event_name LIKE '%".$event_name."%'";
        }
        if($uid!=""){
            $where .= ($where!=""?" AND ":"")."e.uid=".$uid;
        }         
        if($cid!=""){
            $where .= ($where!=""?" AND ":"")."e.cid=".$cid;
        }      
        if($published!="2"){
            $where .= ($where!=""?" AND ":"")."e.published=".$published;
        }
        if($promote!="" && $promote !=EVENT_PROMOTE_VIP){
            $where .= ($where!=""?" AND ":"")."e.promote=".$promote;
        }
        if($promote==EVENT_PROMOTE_VIP){
            $where .= ($where!=""?" AND ":"")."e.end_time_promote>".time()." AND e.total_tcoin>0";
        }


        $sql = "SELECT e.*,u.name, t.tid, t.name AS category_name, nw.name as network_name, nt1.name as country_name, nt2.name as city_name, nt3.name as dictrict_name " .
            "FROM tamtay_event e 
            INNER JOIN tamtay_term t ON e.cid=t.tid 
            INNER JOIN mytt_network nw ON e.network_id=nw.nwid 
            INNER JOIN users u ON e.uid=u.uid " .
            "LEFT JOIN nations nt1 ON e.country=nt1.nid " .
            "LEFT JOIN cities nt2 ON e.city=nt2.cid " .
            "LEFT JOIN districts nt3 ON e.district=nt3.did";  
        $orderby=" ORDER BY changed DESC";
        $sql = $sql.($where!=""?" WHERE ":"").$where.$orderby;  
        

        $this->setRowPerPage($limit);
        return $this->pageQuery($sql, array(), $page);
    }
    
    public function changePromote2($eid,$value){
        $sql = "UPDATE tamtay_event SET promote=?,changed=".time()." WHERE eid=?";
        $db = $this->getMasterDB();
        $db->query($sql,Array($value,$eid));
        }
    public function addModLog($uid, $action, $ref, $note,$module){ 
        $sql="INSERT INTO  mytt_mod_log(uid ,
            action ,
            timestamp ,
            ref ,
            note,
            module
            )
            VALUES (
            ?,?,?,?,?,?
            )";
        $db = $this->getMasterDB();
        $db->query($sql,Array($uid,$action,time(),$ref,$note,$module));
    }
    public function updateAdminEventChanged($eid){
        $sql="UPDATE  tamtay_event SET  admin_event_changed =  ".time()." WHERE  eid =?";
        $db = $this->getMasterDB();
        $db->query($sql,Array($eid));
    }
    public function deleterequest(){
        $sql="DELETE r.* FROM tamtay_request r INNER JOIN tamtay_event e ON r.sender_id=e.eid WHERE r.type=".REQUEST_EVENT_INVITE_TYPE." AND r.created > e.end_time";
        $db = $this->getMasterDB();
        $db->query($sql);
        $sql2="DELETE r.* FROM tamtay_request r INNER JOIN tamtay_event e ON r.receiver_id=e.eid WHERE r.created > e.end_time AND r.type =".REQUEST_EVENT_REQUEST_TYPE;
        $db->query($sql2);
    }
    public function countrequest(){
        $sql="SELECT count(*) FROM tamtay_request r INNER JOIN tamtay_event e ON r.sender_id=e.eid WHERE r.type=".REQUEST_EVENT_INVITE_TYPE." AND r.created > e.end_time";
        $db = $this->getMasterDB();
        $a=$db->fetchOne($sql);
        $sql2="SELECT count(*) FROM tamtay_request r INNER JOIN tamtay_event e ON r.receiver_id=e.eid WHERE r.created > e.end_time AND r.type =".REQUEST_EVENT_REQUEST_TYPE;
        $b=$db->fetchOne($sql2);
        return $a+$b;
    }
    public function getEventName($eid){
        $sql="SELECT event_name FROM tamtay_event WHERE eid=?";
        $db= $this->getMasterDB();
        return $db->fetchOne($sql,Array($eid));
    }
    public function getAllCategory(){
        $sql="SELECT * FROM tamtay_term WHERE type='event'";
        $db= $this->getMasterDB();
        return $db->fetchAll($sql,Array($eid));
    }
    public function changePromoteAndPublished($eid,$promote,$published){
        $sql = "UPDATE tamtay_event SET promote=?,published=?,admin_event_changed=".time()." WHERE eid=?";
        $db = $this->getMasterDB();
        $db->query($sql,Array($promote,$published,$eid));
    }
    public function changeCid($eid,$cid){
        $sql = "UPDATE tamtay_event SET cid=?,admin_event_changed=".time()." WHERE eid=?";
        $db = $this->getMasterDB();
        $db->query($sql,Array($cid,$eid)); 
    }
}